package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.model.UserDeviceinfo;
import com.qqtech.qquan.user.model.UserDeviceinfoQuery;
import com.qqtech.qquan.user.model.UserDeviceinfoVo;
import com.qqtech.qquan.user.service.UserDeviceinfoService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/deviceinfo")
public class UserDeviceinfoController extends BaseWebController<UserDeviceinfo, UserDeviceinfoQuery> {
	@Resource
	private UserDeviceinfoService userDeviceinfoService;

	@Override
	protected BaseService<UserDeviceinfo> getBaseService() {
		return userDeviceinfoService;
	}

	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserDeviceinfoVo userDeviceinfoVo) {
		Result ret = new Result();
		if (StringUtil.isBlank(userDeviceinfoVo.getDeviceId())) {
			return ret;
		}
		Integer curUserId = super.getUserId();
		if (curUserId != null) {
			userDeviceinfoVo.setUserId(curUserId);
		} else {
			userDeviceinfoVo.setUserId(0);
		}
		if (StringUtil.isBlank(userDeviceinfoVo.getPhoneNumber())) {
			userDeviceinfoVo.setPhoneNumber("0");
		}
		UserDeviceinfo userDeviceinfo = new UserDeviceinfo();
		userDeviceinfo.setDeviceId(userDeviceinfoVo.getDeviceId());
		userDeviceinfo.setUserId(userDeviceinfoVo.getUserId());
		userDeviceinfo.setPhoneNumber(userDeviceinfoVo.getPhoneNumber());
		userDeviceinfo = userDeviceinfoService.getOne(userDeviceinfo);
		if (userDeviceinfo == null) {
			userDeviceinfoService.insert(userDeviceinfoVo);
		}
		return ret;
	}
}
